﻿using Learun.Util;
using Learun.Util.SqlSugar;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using DbType = System.Data.DbType;

namespace Learun.Application.Scheduler
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2019-08-13 11:16
    /// 描 述：任务调度
    /// </summary>
    public class Job_ScheduleService
    {
        #region 仓储
        Repository<Job_ScheduleEntity> _jobScheduleRepository => new Repository<Job_ScheduleEntity>();
        #endregion
        #region 获取数据

        /// <summary>
        /// 获取页面显示列表数据
        /// <summary>
        /// <param name="queryJson">查询参数</param>
        /// <returns></returns>
        public IEnumerable<Job_ScheduleEntity> GetPageList(Pagination pagination, string queryJson)
        {
            try
            {
                var strSql = new StringBuilder();
                strSql.Append("SELECT ");
                strSql.Append(@"
                t.id,
                t.JobName,
                t.AliasName,
                t.JobGroup,
                t.Description,
                t.TaskClass,
                t.CromExpress,
                t.RunStatus
                ");
                strSql.Append("  FROM Job_Schedule t ");
                strSql.Append("  WHERE 1=1 ");
                var queryParam = queryJson.ToJObject();
                // 虚拟参数
                //var dp = new DynamicParameters(new { });
                var dp = new List<SugarParameter>();
                if (!queryParam["Keyword"].IsEmpty())
                {
                    strSql.Append(string.Format(" AND (t.JobName Like '%{0}%' OR t.AliasName Like '%{0}%') ",
                        queryParam["Keyword"].ToString()));
                }
                if (!queryParam["RunStatus"].IsEmpty())
                {
                    dp.Add(new SugarParameter("RunStatus", "%" + queryParam["RunStatus"].ToString() + "%", DbType.String));
                    strSql.Append(" AND t.RunStatus Like @RunStatus ");
                }
                //return this.BaseRepository().FindList<Job_ScheduleEntity>(strSql.ToString(), dp, pagination);
                return SqlSugarHelper.Db.SqlQueryable<Job_ScheduleEntity>(strSql.ToString()).AddParameters(dp).ToPageList(pagination.page, pagination.rows);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 获取数据
        /// <summary>
        /// <param name="queryJson">查询参数</param>
        /// <returns></returns>
        public List<Job_ScheduleEntity> GetList(string queryJson)
        {
            try
            {
                var strSql = new StringBuilder();
                strSql.Append("SELECT ");
                strSql.Append(@"
                t.id,
                t.JobName,
                t.AliasName,
                t.JobGroup,
                t.Description,
                t.TaskClass,
                t.CromExpress,
                t.RunStatus
                ");
                strSql.Append("  FROM Job_Schedule t ");
                strSql.Append("  WHERE 1=1 ");
                var queryParam = queryJson.ToJObject();
                // 虚拟参数
                var dp = new List<SugarParameter>();
                if (!queryParam["JobName"].IsEmpty())
                {
                    dp.Add(new SugarParameter("JobName", "%" + queryParam["JobName"].ToString() + "%", DbType.String));
                    strSql.Append(" AND t.JobName Like @JobName ");
                }
                if (!queryParam["AliasName"].IsEmpty())
                {
                    dp.Add(new SugarParameter("AliasName", "%" + queryParam["AliasName"].ToString() + "%", DbType.String));
                    strSql.Append(" AND t.AliasName Like @AliasName ");
                }
                if (!queryParam["JobGroup"].IsEmpty())
                {
                    dp.Add(new SugarParameter("JobGroup", "%" + queryParam["JobGroup"].ToString() + "%", DbType.String));
                    strSql.Append(" AND t.JobGroup Like @JobGroup ");
                }
                if (!queryParam["TaskClass"].IsEmpty())
                {
                    dp.Add(new SugarParameter("TaskClass", "%" + queryParam["TaskClass"].ToString() + "%", DbType.String));
                    strSql.Append(" AND t.TaskClass Like @TaskClass ");
                }
                if (!queryParam["CromExpress"].IsEmpty())
                {
                    dp.Add(new SugarParameter("CromExpress", "%" + queryParam["CromExpress"].ToString() + "%", DbType.String));
                    strSql.Append(" AND t.CromExpress Like @CromExpress ");
                }
                if (!queryParam["RunStatus"].IsEmpty())
                {
                    dp.Add(new SugarParameter("RunStatus", "%" + queryParam["RunStatus"].ToString() + "%", DbType.String));
                    strSql.Append(" AND t.RunStatus Like @RunStatus ");
                }
                //return this.BaseRepository().FindList<Job_ScheduleEntity>(strSql.ToString(), dp).ToList();
                return SqlSugarHelper.Db.SqlQueryable<Job_ScheduleEntity>(strSql.ToString()).AddParameters(dp).ToList();
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 获取Job_Schedule表实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public Job_ScheduleEntity GetJob_ScheduleEntity(string keyValue)
        {
            try
            {
                return _jobScheduleRepository.GetById(keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string keyValue)
        {
            try
            {
                _jobScheduleRepository.Delete(t => t.id == keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string keyValue, Job_ScheduleEntity entity)
        {
            try
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    entity.Modify(keyValue);
                    _jobScheduleRepository.Update(entity);
                }
                else
                {
                    entity.Create();
                    _jobScheduleRepository.Insert(entity);
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

    }
}
